unit KontoRDM;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
  DBClient, StdVcl, FMTBcd, DB, Provider, alProvider, SqlExpr, DBXCommon,
  PoslovanjeSrv_TLB,
  SifrantRDM;

type
  TrdmKonto = class(TrdmSifrant, IrdmKonto)
  private
    { Private declarations }
  protected
    class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;
    function GetSelectCommandText(DataSet: TSQLDataSet): string; override;
    function Get_RazredID(const ProviderName: WideString): Integer; safecall;
    function GetTableName: string; override;
    procedure Copy(const Session: ISession; PoslovnoLetoID: Integer); safecall;
    procedure RefreshRecord(const Session: ISession;
      const ProviderName: WideString; ID: Integer); safecall;
    procedure Renumber(const Session: ISession; const FieldName,
      SortFieldName: WideString; StartValue, EndValue: Integer); safecall;
    procedure SetDefaultExpression(DataSet: TDataSet); override;
    procedure Set_ReadOnly(Value: WordBool); safecall;
    procedure Set_SQLConnection(Value: Integer); safecall;
  public
    { Public declarations }
  end;

var
  KontoFactory: TComponentFactory;

implementation

{$R *.DFM}

procedure TrdmKonto.Copy(const Session: ISession; PoslovnoLetoID: Integer);
var
  DBXTrans : TDBXTransaction;
  Proc: TSQLStoredProc;
begin
  DBXTrans := SQLConnection.BeginTransaction(TDBXIsolations.RepeatableRead);
  try
    Proc := TSQLStoredProc.Create(nil);
    try
      Proc.SQLConnection := SQLConnection;
      Proc.CommandText := 'KON_CPY_KON';
      Proc.ParamByName('PAR_REF_ID_IZVOR').Value := PoslovnoLetoID;
      Proc.ParamByName('PAR_REF_ID_PONOR').Value := Session.Params['POSLOVNO_LETO_ID'];
      Proc.ExecProc;
    finally
      Proc.Free;
    end;
    SQLConnection.CommitFreeAndNil(DBXTrans);
  except
    SQLConnection.RollBackFreeAndNil(DBXTrans);
    raise;
  end;
end;

function TrdmKonto.GetSelectCommandText(DataSet: TSQLDataSet): string;
var
  SQL: TStringList;
begin
  SQL := TStringList.Create;
  try
    if DataSet = dsMain then
    begin
      SQL.Add('select');
        SQL.Add('KON.*,');
        SQL.Add('RAZ.OPIS as RAZRED_OPIS');
      SQL.Add('from KONTO KON');
      SQL.Add('left outer join RAZRED RAZ on (KON.RAZRED_ID = RAZ.ID)');
      SQL.Add('where KON.REF_ID = :POSLOVNO_LETO_ID');
    end;
  finally
    Result := Trim(SQL.Text);
    SQL.Free;
  end;
end;

function TrdmKonto.GetTableName: string;
begin
  Result := 'KON';
end;

function TrdmKonto.Get_RazredID(const ProviderName: WideString): Integer;
begin
  Result := inherited Get_RazredID(ProviderName);
end;

procedure TrdmKonto.RefreshRecord(const Session: ISession;
  const ProviderName: WideString; ID: Integer);
begin
  inherited;
end;

procedure TrdmKonto.Renumber(const Session: ISession; const FieldName,
  SortFieldName: WideString; StartValue, EndValue: Integer);
begin
  inherited;
end;

procedure TrdmKonto.SetDefaultExpression(DataSet: TDataSet);
begin
  inherited;
  if DataSet = dsMain then
  begin
    with DataSet do
    begin
      FieldByName('RAZRED_OBVEZEN').DefaultExpression := '''N''';
    end
  end;
end;

procedure TrdmKonto.Set_ReadOnly(Value: WordBool);
begin
  inherited;
end;

procedure TrdmKonto.Set_SQLConnection(Value: Integer);
begin
  inherited;
end;

class procedure TrdmKonto.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
begin
  if Register then
  begin
    inherited UpdateRegistry(Register, ClassID, ProgID);
    EnableSocketTransport(ClassID);
    EnableWebTransport(ClassID);
  end else
  begin
    DisableSocketTransport(ClassID);
    DisableWebTransport(ClassID);
    inherited UpdateRegistry(Register, ClassID, ProgID);
  end;
end;

initialization
  KontoFactory := TComponentFactory.Create(ComServer, TrdmKonto,
    Class_rdmKonto, ciInternal, tmApartment);
end.
